const app = getApp()
var CONFIG = require("../../config/config.js");
var share = require('../../utils/share.js');
// const ctx = wx.createCanvasContext('myCanvas');
Page({
    data: {
        // 测试权限
        discount_name: '',
        chenorderfalse: false,
        orderprice: '',
        ordertext: '请输入网课订单号', //网课订单号
        is_img: 3, //1:付款截图  2:网课订单号 3：线上支付
        is_area_type: 1,
        is_area_more: 1,
        stu_id: '', //用户id
        truename: '',
        tel: '',
        discounts: [],
        discount_index: 0,
        pay_methods: [{ title: "请选择" }], //缴费方式
        pay_method_index: 0, //缴费方式下标
        // pay_types: [{ title: "请选择" }],
        pay_type_title: '',
        area_team: [], //校区列表
        area_index: 0, //校区下标
        area_id: '', //校区下标
        discountsPrice: '',
        discountsId: '',
        discountsIndex: -1,
        moneyfree: [], //付款方式
        moneyfree_id: 2,
        Imagefalse: false, //付款截图是否显示
        selfprice: 0,
        img: '/images/sign.png',
        imgback: '/images/qikeya1.png',
        title: '',
        text: '',
        imagePath: [],
        imgUrl: CONFIG.API_PIC,
        showerca: false,
        showerca1: false,
        jiaoyanCode: '',
        butfalse: true,
        showfalse: false,
        showfalse1: false,
        text1: '',
        textsan: '请填写备注',
        orderprice: 0,
        isimage: 1, //是否需要上传图片  1：需要 2不需要
        pid: '',
        cate_id: '', //分类id
        course_id: '', //课程id
        specs_id: '', //规格id
        multiIndex: [-1, -1], //课程分类下标
        multiArray: [], //课程分类数组
        multiArraydis: [],
        courseregin1: '', //课程
        courseregin2: '', //课程
        courseIndex: [-1, -1], //课程下标
        courseArray: [], //课程数组
        courselist: [], //当前规格数组
        course_title: '', //课程选中name
        gge_title: '',
        ggelist: '', //当前规格数组
        y_price: 0, //课程原价
        ggetitle: '', //选中规格name
        is_specs: 0, //0没有规格 1有规格
        pay_type: 1, //1一次付清 2分期
        downpayment: 0, //首付金额
        ucp_id: 0, //缴费记录id
        paydetail: '', //缴费详情
        is_anew: 2,
        is_again: 2,
        is_xuancate: 2,
        consult_id: '', //课程顾问
        bu_orderparice: 0,
        is_zycon: 2,
        iszf: 1,
        newconid: '',
        keycourse: '',
        steparr: [{ id: 1, title: '1.课程分类选择' }, { id: 2, title: '2.选择课程' }, { id: 3, title: '3.选择优惠' }],
        is_stepid: 1,
        is_search: 1, //1搜索 2选择
        chooselist: [], //分类二级数据
        regin1: '',
        regin2: '',
        is_scorder: 1, //1还没有生成订单 2生成订单
        keywordlists: [],
        is_searchid: 1, //1分类搜索 2查询，
        childtype:'',
        cate_id1:'',
        photo:'',
        is_photo:0,
        uploadPhoto:'',
        showfalse2:false,
        issuccess:false,
        isfais:false,
        is_check:0,//1可以，0不可以
        is_oneself:1,
        specsList:[],
        attrList:[],
        isspes:false,
        attrInfo:{
            id:'',
            frist_name:'',
            second_name:'',
            price:0
        },
        product:[],
        repeat_price:'',
        frist_nameN:'',
        pPrice:0,
        is_train:0,
        islink:true,
        childList:[]
    },
    inputsearch() {
        var that = this;
        that.setData({
            is_search: 2,
            regin1: '',
            regin2: '',
            chooselist: [],
            "multiIndex[0]":-1,
            "multiIndex[1]": -1,
            cate_id: '',
            cart_title: '',
            courseIndex: [-1, -1],
            courseregin1: '',
            courseregin2: '',
            course_title: '',
            moneyfree_id: 2,
            is_xuancate: 1,
            is_stepid: 1,
        })
        that.keywordcourse();
    },
    keywordcourse() {
        var that = this;
        wx.showLoading({
            title: '加载中~',
        })
        share.wxRequest('crmapi/user/keyword', { status: 2, page: 1, limit: 100 }, function(res) {
            if (Number(res.data.errcode) == 0) {
                var data = res.data.data.lists;
                that.setData({
                    keywordlists: data
                })
            }
            wx.hideLoading();
        })
    },
    keyclick(e) {
        var that = this;
        var item = e.target.dataset.item;
        that.areaCourse(item.name)
    },
    // 点击生成订单
    ordersclick() {
        var that = this;
        // if(product.length>0){
        //     this.getLength()
        // }
        console.log(this.data.product);
        
        if(this.data.product.length==1){
            if(this.data.product[0].is_type==3){
                wx.showToast({
                    title: '往期已报不可单独选择',
                    icon: 'none'
                })
            }else{
                that.setData({
                    is_scorder: 2,
                    isspes:false
                })
            }
        }else{
            let a=0
            this.data.product.forEach(v=>{
                if(v.is_type==3){
                    a++
                }
            })
            if(this.data.islink){
                that.setData({
                    is_scorder: 2,
                    isspes:false
                })
            }else{
                if(a==0){
                    that.setData({
                        is_scorder: 2,
                        isspes:false
                    })
                }else{
                    let attrList=that.data.attrList
                    attrList.forEach(v=>{
                                v.disCount=0
                    })
                    let attrInfo={}
                    that.setData({
                        isspes:false,
                        attrInfo:attrInfo,
                        frist_nameN:'',
                        repeat_price:0,
                        pPrice:0,
                        product:[],
                        attrList:attrList
                    })
                    wx.showToast({
                        title: '当前不允许选择往期已报,请重新选择折扣',
                        icon: 'none'
                    })
                }
            }
        }

    },
    // 点击搜索课程名称
    formSubmit(e) {
        var that = this;
        var keycourseall = e.detail.value.keycourseall;
        that.areaCourse(keycourseall)
    },
    areaCourse(name) {
        var that = this;
        if (name == '') {
            wx.showToast({
                title: '请输入关键字',
                icon: 'none'
            })
        } else {
            wx.showLoading({
                title: '加载中~',
            })
            share.wxRequest('crmapi/user/areaCourse', {
                keyword: name,
                area_id: that.data.area_id,
                user_id: that.data.stu_id,
                limit: 100
            }, function(res) {
                if (Number(res.data.errcode) == 0) {
                    var data = res.data.data;
                    if (Number(data.length) <= 0) {
                        wx.showToast({
                            title: '该搜索下暂无课程',
                            icon: 'none'
                        })
                    } else {
                        that.setData({
                            courseArray: data,
                            is_search: 1,
                            is_stepid: 2,
                            is_searchid: 2,
                        })
                    }
                } else {
                    wx.showToast({
                        title: res.data.msg,
                        icon: 'none'
                    })
                }
                wx.hideLoading();
            })
        }
    },
    areaeChangemore() {
        var that = this;
        that.setData({
            is_area_more: 2
        })
    },
    areaeChangemore1() {
        var that = this;
        that.setData({
            is_area_more: 1
        })
    },

    // 点击输入网课订单号
    orderclick() {
        var that = this;
        that.setData({
            chenorderfalse: true
        })
    },
    // 点击确定订单号
    yesbutclick(e) {
        var that = this;
        wx.showLoading({
            title: '正在查看该订单号~',
        })
        var orderid = e.detail.value.orderid;
        var stu = that.data.stu;
        if (orderid == '') {
            wx.showToast({
                title: '请输入网课订单号',
                icon: 'none'
            })
        } else {
            if (that.data.discountsId == '' || Number(that.data.discountsId) <= 0) {
                var discounts = that.data.y_price
            } else {
                var discounts = that.data.discountsPrice
            }
            var obj = {
                from: 1,
                wkOrderId: orderid,
                user_id: that.data.stu_id,
                consultant_id: app.globalData.admin_id,
                discounts: discounts,
                user_course_id: that.data.course_id,
            }
            if (Number(app.globalData.identity) == 3) {//学管
                if (that.data.newconid != '' && Number(that.data.newconid) > 0) {
                    var url = 'crmapi/user/wkOrder'
                } else {
                    var url = 'crmapi/Learn/wkOrder'
                }
            } else {
                var url = 'crmapi/user/wkOrder'
            }
            share.wxRequest(url, obj, function(res) {
                wx.hideLoading();
                if (Number(res.data.errcode) == 0) {
                    var data = res.data.data;
                    // bu_orderparice
                    if (Number(discounts) - Number(data.res) > 0) {
                        var bu_orderparice = Number(discounts) - Number(data.res);
                    } else {
                        var bu_orderparice = 0;
                    }
                    that.setData({
                        ordertext: orderid,
                        orderprice: data.res,
                        chenorderfalse: false,
                        bu_orderparice: bu_orderparice,
                    })

                } else {
                    wx.showToast({
                        title: res.data.msg,
                        icon: 'none',
                        duration: 2500,
                    })
                }
            })
        }
    },
    repeatclosefree() {
        var that = this;
        that.setData({
            chenorderfalse: false
        })
    },
    closeorder(){
        var that = this;
        that.setData({
            showfalse2: false
        })
    },

    // 校区选择
    areaeChange(e) {
        var that = this;
        var index = e.target.dataset.index;
        that.setData({
            area_index: index,
        })

        if (Number(index) > 0) {
            that.setData({
                area_id: that.data.area_team[index].id
            })
            that.getAreaCourse(1);

        } else {
            that.setData({
                area_id: ''
            })
        }
        that.setData({
            courseIndex: [-1, -1],
            courseregin1: '',
            courseregin2: '',
            course_title: '',
            moneyfree_id: 2,
            is_xuancate: 2,
        })
    },
    closeareae() {
        var that = this;
        let attrInfo={
            id:'',
            frist_name:'',
            second_name:'',
            price:0
        }
        that.setData({
            area_index: 0,
            area_id: '',
            courseIndex: [-1, -1],
            courseregin1: '',
            courseregin2: '',
            course_title: '',
            moneyfree_id: 2,
            is_xuancate: 2,
            is_scorder: 1,
            is_stepid: 1,
            is_search: 1,
            is_searchid:1,
            attrInfo:attrInfo,
            product:[],
            repeat_price:'',
            frist_nameN:'',
            pPrice:0,
            is_train:0
        })
    },
    // 获取校区列表
    teamlist(type) {
        var that = this;
        share.wxRequest('crmapi/User/team', {}, function(res) {
            if (Number(res.data.errcode) == 0) {
                var data = res.data.data;
                var obj = [{
                    id: 0,
                    title: '请选择校区'
                }]
                var data1 = obj.concat(data);
                that.setData({
                    area_team: data1
                })
                if (Number(type) == 2) {
                    var area_id = that.data.area_id;
                    for (var i = 0; i < data1.length; i++) {
                        if (Number(data1[i].id) == Number(area_id)) {
                            that.setData({
                                area_index: i
                            })
                        }
                    }
                }
                that.getAreaCourse(2)
            }
        })
    },
    //生命周期函数--监听页面加载
    onLoad: function(options) {
        let that = this;
        if (options.photo) {
            that.setData({
                photo: options.photo
            })
        }
        if (options.is_check) {
            that.setData({
                is_check: options.is_check
            })
        } 
        if (options.is_oneself) {
            that.setData({
                is_oneself: options.is_oneself
            })
        }
        if (options.newconid) {
            that.setData({
                newconid: options.newconid
            })
        }
        if (options.is_zycon) {
            that.setData({
                is_zycon: options.is_zycon
            })
        } else {
            that.setData({
                is_zycon: 2
            })
        }
        if (Number(options.user_id) <= 0) {
            var user_id = 0;
            var ucp_id = options.ucp_id;
        } else {
            var ucp_id = 0;
            var user_id = options.user_id;
            // 获取付款方式
            that.haveoto(1);
            // 获取校区
            that.teamlist(1);
        }

        that.setData({
            stu_id: user_id,
            truename: options.name,
            tel: options.tel,
            ucp_id: ucp_id
        });

        if (Number(ucp_id) > 0) {
            that.payLogDetails();
        }
    },
    // 单个缴费记录详情
    payLogDetails() {
        var that = this;
        wx.showLoading({
            title: '加载中~',
        })
        if (Number(app.globalData.identity) == 3) {
            if (that.data.newconid != '' && Number(that.data.newconid) > 0) {
                var url = 'crmapi/user/payLogDetails'
            } else {
                var url = 'crmapi/Learn/payLogDetails'
            }
        } else {
            var url = 'crmapi/user/payLogDetails'
        }
        share.wxRequest(url, { ucp_id: that.data.ucp_id }, function(res) {
            if (Number(res.data.errcode) == 0) {
                var data = res.data.data.data;
                if (data.specs_id && Number(data.specs_id) > 0) {
                    var gge_title = data.specs_name
                } else {
                    var gge_title = '';
                }
                var jietu = data.jietu;
                if (jietu != '') {
                    var arr = jietu.split(',');
                } else {
                    var arr = [];
                }
                if (data.discount_name == '') {
                    var discount_name = '无折扣'
                } else {
                    var discount_name = data.discount_name
                }
                let attrInfo={
                    frist_name:discount_name,
                    price:data.discounts,
                    second_name:'',
                    id:data.discount_id
                }
                that.setData({
                    is_scorder: 2,
                    paydetail: data,
                    cart_title: data.cate_name+data.cate_name2,
                    is_anew: data.is_anew, //是否可以再次付款  1:不可以  2:可以
                    is_again: data.is_again, //是否可以重新选课  1:不可以  2:可以 （is_anew等于2的时候才有该字段）
                    gge_title: gge_title,
                    y_price: data.y_discounts,
                    cate_id: data.pid,
                    cate_id1: data.pid,
                    course_id: data.course_id, //课程id
                    specs_id: data.specs_id, //规格id
                    discountsId: data.discount_id,
                    discount_name: discount_name,
                    stu_id: data.user_id,
                    imagePath: arr,
                    regin1: data.cate_name,
                    regin2: data.cate_name,
                    // regin: [data.cate_name, data.cate_name],
                    courseregin1: data.course_name,
                    courseregin2: data.course_name,
                    pay_type: data.pay_type,
                    downpayment: data.downpayment,
                    discountsPrice: data.discounts,
                    is_xuancate: 1,
                    course_title: data.course_name,
                    area_id: data.area_id,
                    iszf: data.oto_type,
                    attrInfo:attrInfo,
                    is_train:data.is_train,

                })
                if(data.is_train==3||data.is_train==4){
                    wx.showToast({
                        title: '技能提升连报折扣请重新选课',
                        icon: 'none'
                    })
                    that.closeareae()
                }
                // that.cateCourse();
                // 获取付款方式
                that.haveoto(2);
                //获取折扣
                // that.courseDiscount();
                // 获取校区
                that.teamlist(2);
                
            }
            wx.hideLoading();
        })
    },
    // 获取付款方式
    haveoto(type) {
        var that = this;
        // 获取付款方式
        share.courseotoType(function(res) {
            if (Number(res.data.errcode) == 0) {
                var data1 = res.data.data;
                var obj = [{
                    "id": 0, //付款方式ID
                    "oto_name": "请选择付款方式"
                }]
                // if (Number(that.data.is_zycon) == 1 || Number(that.data.is_zycon) == 3) {
                //     var objdata = [{
                //         "id": 2, //付款方式ID
                //         "oto_name": "线上付款"
                //     }]
                //     var data = obj.concat(objdata)
                // } else {
                var data = obj.concat(data1)
                // }
                that.setData({
                    moneyfree: data
                })
                if (Number(type) == 2) {
                    var oto_type = that.data.paydetail.oto_type;
                    for (var i = 0; i < data.length; i++) {
                        if (Number(oto_type) == Number(data[i].id)) {
                            that.setData({
                                moneyfree_id: data[i].id
                            })
                        }
                    }
                    if (Number(oto_type) == 1) {
                        that.getPayMethods();
                    }
                } else {
                    that.setData({
                        moneyfree_id: 2
                    })
                }
            }
        })
    },
    catedelChange(e) {
        var that = this;
        let attrInfo={
            id:'',
            frist_name:'',
            second_name:'',
            price:0
        }
        var index = e.target.dataset.index || e.currentTarget.dataset.index;
        if (Number(index) == 1) {
            that.setData({
                regin1: '',
                regin2: '',
                chooselist: [],
                "multiIndex[0]": -1,
                "multiIndex[1]": -1,
                cate_id: '',
                cate_id1:'',
                cart_title: '',
                courseIndex: [-1, -1],
                courseregin1: '',
                courseregin2: '',
                course_title: '',
                moneyfree_id: 2,
                is_xuancate: 1,
                is_stepid: 1,
                attrInfo:attrInfo,
                product:[],
                repeat_price:'',
                frist_nameN:'',
                pPrice:0,
                is_train:0
            })
        } else if (Number(index) == 2) {
            that.setData({
                cart_title: that.data.regin1,
                regin2: '',
                cate_id: '',
                // cate_id1:'',
                "multiIndex[1]": -1,
                is_xuancate: 1,
                is_stepid: 1,
                courseIndex: [-1, -1],
                courseregin1: '',
                courseregin2: '',
                course_title: '',
                attrInfo:attrInfo,
                product:[],
                repeat_price:'',
                frist_nameN:'',
                pPrice:0,
                is_train:0
            })
        } else if (Number(index) == 3) {
            if(that.data.childList==0){
                that.setData({
                    // courselist: [],
                    discount_name: '',
                    pay_type: '',
                    downpayment: '',
                    course_id: '',
                    discountsId: '',
                    discountsIndex: -1,
                    y_price: '',
                    course_title: '',
                    courseregin1: '',
                    "courseIndex[0]": -1,
                    "courseIndex[1]": -1,
                    is_stepid: 2,
                    attrInfo:attrInfo,
                    product:[],
                    repeat_price:'',
                    frist_nameN:'',
                    pPrice:0,
                    is_train:0
                })

            }else{
                that.setData({
                    courselist: [],
                    discount_name: '',
                    pay_type: '',
                    downpayment: '',
                    course_id: '',
                    discountsId: '',
                    discountsIndex: -1,
                    y_price: '',
                    course_title: '',
                    courseregin1: '',
                    "courseIndex[0]": -1,
                    "courseIndex[1]": -1,
                    is_stepid: 1,
                    attrInfo:attrInfo,
                    product:[],
                    repeat_price:'',
                    frist_nameN:'',
                    pPrice:0,
                    is_train:0
                })
            }
        }
        that.setData({
            is_scorder: 1,
        })
    },
    changecourseall() {
        var that = this;
        that.setData({
            regin1: '',
            regin2: '',
            chooselist: [],
            "multiIndex[0]": -1,
            "multiIndex[1]": -1,
            cate_id: '',
            cate_id1:'',
            cart_title: '',
            courseIndex: [-1,-1],
            courseregin1: '',
            courseregin2: '',
            course_title: '',
            moneyfree_id: 2,
            is_xuancate: 1,
            is_stepid: 1,
            is_scorder: 1,
            product:[]
        })
    },
    // 选择课程分类
    catefirstChange(e) {
        var that = this;
        var index = e.target.dataset.index;
        var item = e.target.dataset.item;
        if (item.child && Number(item.child.length) > 0) {
            var child = item.child;
            var cate_id = '';
            var is_xuancate = 1;
            var is_stepid = 1;
        } else {
            var child = [];
            var cate_id = item.id;
            var is_xuancate = 2;
            var is_stepid = 2;
        }
        var cate_id1=item.id
        that.setData({
            regin1: item.title,
            chooselist: item.child,
            "multiIndex[0]": index,
            "multiIndex[1]": -1,
            cate_id: cate_id,
            cate_id1:cate_id1,
            cart_title: item.title,
            courseIndex: [-1, -1],
            courseregin1: '',
            courseregin2: '',
            course_title: '',
            moneyfree_id: 2,
            is_xuancate: is_xuancate,
            is_stepid: is_stepid,
            childList:child
        })
        // if (cate_id != '') {
        //     that.cateCourse();
        // }
        if(child.length==0){
            that.cateCourse1();

        }
    },
    // /选择二级分类
    catesecChange(e) {
        var that = this;
        var index = e.target.dataset.index;
        var item = e.target.dataset.item;
        var cart_title = that.data.cart_title + ',' + item.title;
        that.setData({
            cart_title: cart_title,
            regin2: item.title,
            cate_id: item.id,
            "multiIndex[1]": index,
            is_xuancate: 2,
            is_stepid: 2,
        })
        that.cateCourse1();
    },
    courseChange(e){
        let that=this
        console.log(e);
        let item=e.target.dataset.item
        that.setData({
            course_id: item.course_id,
            course_title: item.course_name,
            courseregin1: item.course_name,
            downpayment:item.downpayment
        })
        if(that.data.cate_id1=='162'){
            that.courseDetail1(item.course_id)

        }else{
            that.courseDetail(item.course_id)
        }
    },
    courseDetail(id) {
        var that = this;
        wx.showLoading({
            title: '加载中~',
        })
        var formData = {
            course_id: id,
        }
        share.wxRequest('crmapi/Market/courseAttr', formData, function(res) {
            if (Number(res.data.errcode) == 0) {
                let data=res.data.data.attr
                let arr=[]
                let specName=''
                let arr1=[]
                let specName1=''
                let specsList=[]
                data.forEach(val=>{
					if(arr.includes(val.frist_name)){				
					}else{
						arr.push(val.frist_name)	
					}
					if(arr1.includes(val.second_name)){				
					}else{
						arr1.push(val.second_name)	
					}
					if(val.frist_attr_name){
						specName=val.frist_attr_name
					}
					if(val.second_attr_name){
						specName1=val.second_attr_name
					}
				})
                if(arr.length!=0&&specName!=''){
					let obj={
						specName:specName,
						spec:[],
					}
					arr.forEach(v=>{
						let objs={
								name:v,
						}
						obj.spec.push(objs)
					})
					specsList[0]=obj
				}
				if(arr1.length!=0&&specName1!=''){
					let obj={
						specName:specName1,
						spec:[],
					}
					arr1.forEach(v=>{
						let objs={
								name:v,
						}
						obj.spec.push(objs)
					})
					specsList[1]=obj
				}
                that.setData({
                    isspes:true,
                    specsList: specsList,
                    attrList:data,
                    downpayment:res.data.data.downpayment
                })
            }
            wx.hideLoading()
        })
    },
    courseDetail1(id) {
        var that = this;
        wx.showLoading({
            title: '加载中~',
        })
        var formData = {
            course_id: id,
        }
        share.wxRequest('crmapi/Market/courseAttr', formData, function(res) {
            if (Number(res.data.errcode) == 0) {
                let data=res.data.data.attr
                data.forEach(v=>{
                    v.disCount=0
                })
                that.setData({
                    is_stepid:3,
                    attrList:data
                })
            }
            wx.hideLoading()
        })
    },
    getDisCount(e){
        let attrList=this.data.attrList
        let index=e.target.dataset.index
        let disCount=e.target.dataset.type
        let oldDisCount=attrList[index].disCount
        let product=[]
        let attrInfo =this.data.attrInfo
        let is_train =this.data.is_train
        if(oldDisCount==disCount&&oldDisCount!=0){
            attrList[index].disCount=0
        }else{
            attrList[index].disCount=disCount
        }
        attrList.forEach(v=>{
            if(v.disCount!=0){
                let obj={
                    id:v.id,
                    is_type:v.disCount
                }
                product.push(obj)
            }
        })
        let repeat_price=0
        let frist_nameN=''
        let pPrice=''
        let y_price=''
        attrList.forEach(v=>{
            if(v.disCount==2||v.disCount=='2'){
                repeat_price=Number(repeat_price)+Number(v.repeat_price)
            }
            if(v.disCount!=0&&v.disCount!='0'){
                frist_nameN=frist_nameN+v.frist_name,
                pPrice=Number(pPrice)+Number(v.price)
            }
        })
        this.setData({
            product:product,
            attrList:attrList,
            repeat_price:repeat_price,
            frist_nameN:frist_nameN,
            pPrice:pPrice,
            isspes:false
        })
        if(product.length>0){
            this.getLength()
        }
    },
    getLength(){
        // if(this.data.product.length==1){
        //     if(this.data.product[0].is_type==3){
        //         wx.showToast({
        //             title: '往期已报不可单独选择',
        //             icon: 'none'
        //         })
        //     }else{
        //         this.checkAttrRestrict()
        //     }
        // }else{
            this.checkAttrRestrict()
        // }
    },
    checkAttrRestrict() {
        var that = this;
        // wx.showLoading({
        //     title: '加载中~',
        // })
        var formData = {
            course_id: that.data.course_id,
            pid:that.data.cate_id,
            product:that.data.product
        }
        share.wxRequest('crmapi/Market/checkAttrRestrict', formData, function(res) {
                let attrInfo={}
                let repeat_price=that.data.repeat_price
                let frist_nameN=that.data.frist_nameN
                let pPrice=that.data.pPrice
                let attrList=that.data.attrList
                let is_train=that.data.is_train
                if(res.data.msg=='连报不存在'||res.data.msg=='错误'){
                    if(that.data.product.length==1){
                        attrInfo.id=formData.product[0].id
                        is_train=formData.product[0].is_type
                        attrInfo.frist_name=frist_nameN
                        attrInfo.second_name=''
                        attrInfo.price=Number(pPrice)+Number(repeat_price)
                        that.setData({
                            attrInfo:attrInfo,
                            is_train:is_train,
                            discountsId:attrInfo.id,
                            islink:false
                        })
                    }else{
                        let ids=[]
                        let a=0
                        formData.product.forEach(v=>{
                            if(v.is_type==3){
                                a++
                            }else{
                                ids.push(v.id)
                            }
                        })
                        // if(a==0){
                            let id=ids.join(',')
                            attrInfo.id=id
                            attrInfo.frist_name=frist_nameN
                            attrInfo.second_name=''
                            attrInfo.price=Number(pPrice)+Number(repeat_price)
                            that.setData({
                                attrInfo:attrInfo,
                                is_train:4,
                                discountsId:attrInfo.id,
                                islink:false
                            })
                        // }else{
                        //     attrList.forEach(v=>{
                        //         v.disCount=0
                        //     })
                        //     that.setData({
                        //         isspes:false,
                        //         attrInfo:attrInfo,
                        //         frist_nameN:'',
                        //         repeat_price:0,
                        //         pPrice:0,
                        //         product:[],
                        //         attrList:attrList
                        //     })
                        //     wx.showToast({
                        //         title: '当前不允许选择往期已报,请重新选择折扣',
                        //         icon: 'none'
                        //     })

                        // }
                    }
                }
                if(res.data.msg.indexOf('名额已满')!=-1){
                    // 需要用户重新选
                    attrList.forEach(v=>{
                        v.disCount=0
                    })
                    that.setData({
                        isspes:false,
                        attrInfo:attrInfo,
                        frist_nameN:'',
                        repeat_price:0,
                        pPrice:0,
                        product:[],
                        attrList:attrList,
                        islink:false,
                        is_train:0,
                    })
                    wx.showToast({
                        title: res.data.msg+',请重新选择折扣',
                        icon: 'none'
                    })
                    // attrInfo.id=''
                    // attrInfo.frist_name=frist_nameN
                    // attrInfo.price=Number(pPrice)+Number(repeat_price)
                }
                if(res.data.errcode==0){
                    attrInfo.id=res.data.data.id
                    attrInfo.frist_name=res.data.data.frist_name
                    attrInfo.second_name=''
                    attrInfo.price=Number(res.data.data.price)+Number(repeat_price)
                    that.setData({
                        // isspes:true,
                        attrInfo:attrInfo,
                        discountsId:attrInfo.id,
                        islink:true,
                        is_train:3
                    })
                }
                // if(res.data.errcode==1002){
                //     that.setData({
                //         isspes:false,
                //     })
                // }
            // }
            // wx.hideLoading()
        })
    },
    closeSpecs(){
        let attrInfo={
            id:'',
            frist_name:'',
            second_name:'',
            price:0
        }
        this.setData({
            isspes:false,
            attrInfo:attrInfo,
            product:[],
            repeat_price:'',
            frist_nameN:'',
            pPrice:0,
            is_train:0
        })

    },
    getspecs(e){
        let item=e.target.dataset.item
        let specsList=this.data.specsList
        let attrInfo=this.data.attrInfo
        let attrList=this.data.attrList
        let arr=[]
        specsList.forEach((v,index)=>{
            v.spec.forEach(val=>{
                if(val.name==item.name){
                    if(index==0){
                        attrInfo.id=''
                        attrInfo.price=0
                        attrInfo.frist_name=item.name
                        attrInfo.second_name=''
                        attrList.forEach(a=>{
                            if(a.frist_name==attrInfo.frist_name){
                                console.log(a.second_name);
                                let obj={
                                    name:a.second_name
                                }
                                arr.push(obj)
                            }
                        })
                        if(specsList.length==2){
                            specsList[1].spec=arr
                        }
                    }
                    if(index==1){
                        attrInfo.second_name=item.name
                    }
                }else{
                }
            })
        })
        attrList.forEach(a=>{
            if(specsList.length>=2){
                if(attrInfo.frist_name==a.frist_name&&attrInfo.second_name==a.second_name){
                    attrInfo.id=a.id
                    attrInfo.price=a.price
                }
            }else{
                if(attrInfo.frist_name==a.frist_name){
                    attrInfo.id=a.id
                    attrInfo.price=a.price
                }

            }
        })
        console.log(attrInfo);
        
        this.setData({
            specsList: specsList,
            attrInfo: attrInfo,
            isspes:true,
            discountsId:attrInfo.id
        })
        console.log(this.data.specsList);
    },
    catesecChange1(e){
        var that = this;
        var index = e.target.dataset.index;
        var item = e.target.dataset.item;
        let regin1=that.data.regin1
        let cate_id=this.data.cate_id
        that.setData({
            cart_title: regin1+item.title,
            cate_id: cate_id==item.id?'':item.id,
            "multiIndex[1]": index,
            is_xuancate: 2,
            is_stepid: 1,
        })
        if(cate_id!=item.id){
            that.cateCourse1();
        }
    },
    // 获取课程分类
    getAreaCourse(type) {
        var that = this;
        wx.showLoading({
            title: '加载中~',
        })
        that.setData({
            multiIndex: [-1, -1], //课程分类下标
            multiArray: [], //课程分类数组
            chooselist: [],
            regin1: '',
            regin2: '',
        })
        if (Number(type) == 1) {
            that.setData({
                is_area_type: 2,
            })
        }
        share.wxRequest('crmapi/Market/cateList', { area_id: that.data.area_id }, function(res) {
            if (Number(res.data.errcode) == 0) {
                var data1 = res.data.data.lists;
                that.setData({
                    multiArray: data1,
                })
            }
            wx.hideLoading();
        })
    },

    // 选择课程
    choosecourseclick(e) {
        var that = this;
        var index = e.target.dataset.index;
        var item = e.target.dataset.item;
        var is_photo=e.target.dataset.item.is_photo
        var data = []
        if(that.data.cate_id1==162){

        }else{
            data = [
                    {
                    discount_title: '无折扣',
                    discount_price: item.price,
                    discount_id: '',
                    type:that.data.childtype
                }
            ]
        }
        if (item.child && Number(item.child.length) > 0) {
            var child = item.child;
            var is_xuancate = 1;
            var is_stepid = 1;
        } else {
            var child = [];
        }
        that.setData({
            courselist: data.concat(child),
            discount_name: '',
            pay_type: item.pay_type,
            downpayment: item.downpayment,
            course_id: item.id,
            discountsId: '',
            discountsIndex: -1,
            y_price: item.price,
            discountsPrice: item.price,
            course_title: item.title,
            courseregin1: item.title,
            "courseIndex[0]": index,
            "courseIndex[1]":-1,
            is_stepid: 3,
            is_photo:is_photo
        })
        if (Number(that.data.is_searchid) == 2) {
            if (item.pptitle != '') {
                var cart_title = item.ptitle + ',' + item.pptitle
            } else {
                var cart_title = item.ptitle
            }
            that.setData({
                // regin1: item.ptitle,
                // regin2:item.pptitle,
                cate_id: item.pid,
                cart_title: cart_title,
            })
        }
    },
    // 选择折扣
    choosediscountclick(e) {
        var that = this;
        var index = e.currentTarget.dataset.index;
        var item = e.currentTarget.dataset.item;
        that.setData({
            discountsId: item.discount_id,
            discount_name: item.discount_title,
            discountsIndex: index,
            discountsPrice: item.discount_price
        })
    },

    // 获取所选课程
    cateCourse() {
        var that = this;
        wx.showLoading({
            title: '加载中~',
        })
        var formData = {
            pid: that.data.cate_id,
            area_id: that.data.area_id,
            user_id: that.data.stu_id,
        }
        share.wxRequest('crmapi/user/courseDiscountNew', formData, function(res) {
            if (Number(res.data.errcode) == 0) {
                var data1 = res.data.data;
                var newarr = [];
                // "status": 0,//1:可用 0:禁用 "sale_state": 2,//0停用 1可报名 2满员 "is_exceed": 0,//是否可超员
                for (var i = 0; i < data1.length; i++) {
                    if (Number(data1[i].status) == 1 && (Number(data1[i].sale_state) == 2 && (Number(data1[i].is_exceed) == 1) || Number(data1[i].sale_state) == 1)) {
                        newarr.push(data1[i]);
                    }
                }
                that.setData({
                    courseArray: newarr,
                    courselist: newarr,
                    ggelist: [],
                })
                if (Number(that.data.ucp_id) > 0 && Number(that.data.is_xuancate) == 1) {

                } else {
                    that.setData({
                        pay_type: 1,
                        downpayment: 0, //首付金额
                    })
                }
                if (Number(newarr.length) < 1) {
                    wx.showToast({
                        title: '该分类下暂无课程',
                        icon: 'none'
                    })
                }
            }
            wx.hideLoading()
        })
    },
    cateCourse1() {
        var that = this;
        wx.showLoading({
            title: '加载中~',
        })
        var formData = {
            pid: that.data.cate_id,
            area_id: that.data.area_id,
        }
        share.wxRequest('crmapi/Market/cateCourse', formData, function(res) {
            if (Number(res.data.errcode) == 0) {
                var data1 = res.data.data.lists;
                let arr=that.data.chooselist
                arr.forEach(val => {
                    if(val.id==that.data.cate_id){
                        val.courseList=data1
                    }
                });
                that.setData({
                    chooselist: arr,
                    courseArray: data1,
                    courselist: data1,
                    ggelist: [],
                })
                if (Number(that.data.ucp_id) > 0 && Number(that.data.is_xuancate) == 1) {

                } else {
                    that.setData({
                        pay_type: 1,
                        downpayment: 0, //首付金额
                    })
                }
                if (Number(data1.length) < 1) {
                    wx.showToast({
                        title: '该分类下暂无课程',
                        icon: 'none'
                    })
                }
            }
            wx.hideLoading()
        })
    },
    // getDiscountPrice() {
    //     var that = this;
    //     var formData = {
    //         course_id: that.data.course_id, //	是	int	课程ID
    //         discount_id: that.data.discountsId, //	int	折扣ID
    //         specs_id: that.data.specs_id, //	否	int	规格ID
    //     }
    //     share.wxRequest('crmapi/user/getDiscountByPrice', formData, function(res) {
    //         if (Number(res.data.errcode) == 0) {
    //             var data = res.data.price;
    //             that.setData({
    //                 discountsPrice: data,
    //             })
    //         }
    //     })
    // },


    coursetap() {
        var that = this;
        that.setData({
            showerca: true,
        })
    },
    /// 双击
    doubleTap: function(e) {
        var that = this
        that.setData({
            showerca: false,
        })
    },
    preView(){
        let img=this.data.photo?this.data.photo:this.data.uploadPhoto
        var jiaoyanCode = this.data.imgUrl+img;
        share.sendImage(jiaoyanCode);
    },
    uploadImage3: function() {
        let that = this;
        wx.showLoading({
            title: '上传中~',
        })
        wx.chooseImage({
            count: 1,
            sizeType: ['compressed'],
            sourceType: ['album'],
            success: function(res) {
                wx.showLoading({
                    title: '上传中~',
                })
                var photoSrc = res.tempFilePaths[0];
                share.uploadpic(photoSrc, function(res) {
                    var path = res.data.path;
                    that.setData({
                        uploadPhoto: path
                    })
                    wx.showToast({
                        title: '上传成功',
                        icon: 'none'
                    })
                    wx.hideLoading()
                }, function(res) {
                    wx.showToast({
                        title: '上传失败，请重新上传',
                        icon: 'none'
                    })
                    wx.hideLoading()
                })
            }
        })
    },






    /// 长按
    longTap: function(e) {
        var jiaoyanCode = this.data.jiaoyanCode;
        share.sendImage(jiaoyanCode);
    },


    // 选择付款方式
    moneyfreeChange: function(e) {
        var that = this;
        var index = e.currentTarget.dataset.index;
        that.setData({
            moneyfree_id: that.data.moneyfree[index].id,
            bu_orderparice: 0,
        })
        if (Number(index) == 1) {
            that.getPayMethods();
        }
    },



    // 获取缴费方式
    getPayMethods() {
        var that = this;
        let url=''
        var formData = {
            // discount_id: that.data.discountsId
            discount_id: ''
        }
        let istype=1
        if(Number(app.globalData.is_dl)==1){
            url='crmapi/User/payMethodDl'
            if(Number(app.globalData.role.consult_id)==(that.data.area_id)){
                // console.log('代理自己校区');
                istype=1
            }else{
                // console.log('代理别人校区');
                istype=2
            }
            formData.type=istype
        }else{
            url='crmapi/user/payMethod'
        }
        share.wxRequest(url, formData, function(res) {
            if (Number(res.data.errcode) == 0) {
                var data = res.data.data;
                var obj = [{ title: "请选择" }];
                var methods = obj.concat(data);
                that.setData({
                    pay_methods: methods
                })
                if (Number(that.data.ucp_id) > 0 && Number(that.data.is_xuancate) == 1) {
                    var paydetail = that.data.paydetail;
                    var pay_method = paydetail.pay_method;
                    for (var i = 0; i < methods.length; i++) {
                        if (Number(pay_method) == Number(methods[i].id)) {
                            that.setData({
                                pay_method_index: i,
                                is_img: methods[i].is_img
                            })
                        }
                    }
                }
            }
        })
    },

    payMethodChange: function(e) {
        var that = this;
        var index = e.detail.value;
        var pay_methods = that.data.pay_methods;
        that.setData({
                pay_method_index: index,
                is_img: pay_methods[index].is_img
            })
            // var pay_method_index = 
        if (Number(index) == 0) {
            wx.showToast({
                title: '请选择缴费方式',
                icon: 'none'
            })
        }
    },



    bindinputplace(e) {
        var that = this;
        var price = e.detail.value;
        that.setData({
            selfprice: price,
        })
    },

    // 用户点击右上角分享
    onShareAppMessage: function() {
        return {
            title: CONFIG.SHARETITLE,
            path: CONFIG.PATHLOGIN
        }
    },

    //   删除图片
    uploadImage1(e) {
        var that = this;
        var index = e.target.dataset.index;
        var imagePath = that.data.imagePath;
        imagePath.splice(index, 1)
        that.setData({
            imagePath: imagePath
        })
    },
    // 上传图片
    uploadImage: function() {
        let that = this;
        wx.chooseImage({
            count: 1,
            sizeType: ['compressed'],
            sourceType: ['album'],
            success: function(res) {
                var photoSrc = res.tempFilePaths[0];
                share.uploadpic(photoSrc, function(res) {
                    var path = res.data.path;
                    var imagePath = that.data.imagePath;
                    imagePath.push(path);
                    that.setData({
                        imagePath: imagePath
                    })
                }, function(res) {
                    wx.showToast({
                        title: '上传失败，请重新上传',
                        icon: 'none'
                    })
                })
            }
        })
    },

    closeImage() {
        var that = this;
        that.setData({
            showerca: false,
            showerca1: false,
        })
        wx.setStorageSync('followtype', 3);
        wx.navigateBack();
    },



    // 提交
    bindFormSubmit: function(e) {
        let that = this;
        var textarea = e.detail.value.textarea;
        if (that.data.cate_id == '') {
            wx.showToast({
                title: '请选择课程分类',
                icon: 'none'
            })
        } else if (Number(that.data.area_index) == 0 && Number(that.data.area_team.length) > 1) {
            wx.showToast({
                title: '请选择校区',
                icon: 'none'
            })
        } else if (that.data.courseregin1 == '') {
            wx.showToast({
                title: '请选择所选课程',
                icon: 'none'
            })
        } else if (Number(that.data.moneyfree_id) == 0) {
            wx.showToast({
                title: '请选择付款方式',
                icon: 'none'
            })
        } else {
            // 线上 2  线下1
            var moneyfree_id = this.data.moneyfree_id;
            if (Number(moneyfree_id) == 1) {
                if (Number(this.data.pay_method_index) == 0) {
                    wx.showToast({
                        title: '请选择缴费方式',
                        icon: 'none'
                    })
                } else {
                    var pay_method = that.data.pay_methods[that.data.pay_method_index].id;
                    if (Number(that.data.is_img) == 1) {
                        if (that.data.imagePath == '') {
                            wx.showToast({
                                title: '请上传付款截图',
                                icon: 'none'
                            })
                        } else {
                            that.sharepan(textarea, e.detail.value.amount)
                        }
                    }else if (Number(that.data.is_img) == 3&&pay_method==19) {
                        let amount=this.data.selfprice
                        var reg = /(^[-+]?[1-9]\d*(\.\d{1,2})?$)|(^[-+]?[0]{1}(\.\d{1,2})?$)/;
                        if (amount == '') {
                            wx.showToast({
                                title: '请输入缴费金额',
                                icon: 'none'
                            })
                        } else if (Number(amount) <= 0) {
                            wx.showToast({
                                title: '缴费金额不能为0',
                                icon: 'none'
                            })
                        } else if (!reg.test(amount)) {
                            wx.showToast({
                                title: '请输入正确的缴费金额',
                                icon: 'none'
                            })
                        }else{
                            that.setData({
                                showfalse2:true,
                            })
                        }

                    } else {
                        var ordertext = that.data.ordertext;
                        var orderprice = that.data.orderprice;
                        if (that.data.discountsId && (that.data.discountsId) > 0) {
                            var price = that.data.discountsPrice;
                        } else {
                            var price = that.data.y_price;
                        }
                        if (ordertext == '请输入网课订单号') {
                            wx.showToast({
                                title: '请输入网课订单号',
                                icon: 'none'
                            })
                        } else if (Number(orderprice) > price) {
                            wx.showToast({
                                title: '网课价格不能大于折扣价',
                                icon: 'none'
                            })
                        } else {
                            this.insertUserCourse(textarea, orderprice, '')
                        }
                    }
                }
            } else {
                that.sharepan(textarea, e.detail.value.amount)
            }
        }

    },
    openorder(){
        let textarea=this.data.textsan
        let orderprice=this.data.selfprice
        this.sharepan(textarea, orderprice)
        // this.insertUserCourse(textarea, orderprice)
    },
    sharepan(textarea, amount) {
        var that = this;
        var reg = /(^[-+]?[1-9]\d*(\.\d{1,2})?$)|(^[-+]?[0]{1}(\.\d{1,2})?$)/;
        if (amount == '') {
            wx.showToast({
                title: '请输入缴费金额',
                icon: 'none'
            })
        } else if (Number(amount) <= 0) {
            wx.showToast({
                title: '缴费金额不能为0',
                icon: 'none'
            })
        } else if (!reg.test(amount)) {
            wx.showToast({
                title: '请输入正确的缴费金额',
                icon: 'none'
            })
        }
        // else if((Number(that.data.is_photo) == 1&&!that.data.photo)&&(Number(that.data.is_photo) == 1&&!that.data.uploadPhoto)){
        //     wx.showToast({
        //         title: '当前课程需要上传电子证件照，请上传后再提交',
        //         icon: 'none'
        //     })
        // }
         else if (Number(that.data.is_again) == 2) {
            if (Number(that.data.pay_type) == 2) {
                if (Number(amount) >= Number(that.data.downpayment)) {
                    this.insertUserCourse(textarea, amount, '')
                } else {
                    wx.showToast({
                        title: '缴费金额不能小于定金',
                        icon: 'none'
                    })
                }
            } else {
                if (that.data.discountsId != '' && Number(that.data.discountsId) > 0) {
                    // if (Number(amount) >= Number(that.data.discountsPrice)) {
                        this.insertUserCourse(textarea, amount, '')
                    // } else {
                    //     wx.showToast({
                    //         title: '缴费金额不能小于折扣价',
                    //         icon: 'none'
                    //     })
                    // }
                } else {
                    // if (Number(amount) >= Number(that.data.y_price)) {
                        this.insertUserCourse(textarea, amount, '')
                    // } else {
                    //     wx.showToast({
                    //         title: '缴费金额不能小于课程价格',
                    //         icon: 'none'
                    //     })
                    // }
                }
            }
        } else {
            this.insertUserCourse(textarea, amount, '')
        }
    },


    insertUserCourse(textarea, amount) {
        var that = this;

        var moneyfree_id = that.data.moneyfree_id;
        if (Number(moneyfree_id) == 1) {
            var pay_method = that.data.pay_methods[that.data.pay_method_index].id;
            if (Number(that.data.is_img) == 1) {
                var imagePath = that.data.imagePath
                if (Number(imagePath.length) > 0) {
                    var str = imagePath.join(',');
                } else {
                    var str = '';
                }
            } else {
                var str = '';
            }
        } else {
            var pay_method = '';
            var str = '';
        }
        if (amount == '') {
            var amount1 = 0
        } else {
            var amount1 = Number(amount);
        }
        if (that.data.discountsId == '' || Number(that.data.discountsId) <= 0) {
            var discounts = that.data.y_price
        } else {
            var discounts = that.data.discountsPrice
        }
        if (Number(that.data.ucp_id) > 0) {
            // "is_anew": 2,//是否可以再次付款  1:不可以  2:可以
            // "is_again": 1//是否可以重新选课  1:不可以  2:可以 （is_anew等于2的时候才有该字段）
            if (Number(that.data.is_again) == 1) {
                that.setData({
                    butfalse: false,
                })
                wx.showLoading({
                    title: '提交中~',
                    mask: true
                })
                var params = {
                    area_id: that.data.area_id,
                    ucp_id: that.data.ucp_id, //	是	int	缴费记录表ID
                    amount: amount1, //	是	int	付款金额(包含学杂费)	是	int	付款金额(包含学杂费)
                    oto_type: moneyfree_id, //	是	int	付款方式ID
                    pay_method: pay_method, //	否	int	缴费方式 如果oto_type值为1 该参数必传
                    jietu: str, //	是	string	截图 如果oto_type值为1 该参数必传
                    wkOrderId: that.data.ordertext
                }
                // if (Number(app.globalData.identity) == 3) {
                //     if (that.data.newconid != '' && Number(that.data.newconid) > 0) {
                //         var url = 'crmapi/user/againBukuan'
                //     } else {
                //         var url = 'crmapi/Learn/againBukuan'
                //     }
                // } else {
                //     var url = 'crmapi/user/againBukuan'
                // }
                var url = 'crmapi/Market/againBukuan'
                that.insertUserCourseNew(params, moneyfree_id, url, 1);
            } else {
                if (textarea == '') {
                    wx.showToast({
                        title: '请填写备注',
                        icon: 'none'
                    })
                } else {
                    that.setData({
                        butfalse: false,
                    })
                    wx.showLoading({
                        title: '提交中~',
                        mask: true
                    })
                    let discount_id=''
                    let discount_name=''
                    let is_train=that.data.is_train
                    let discount_id2=''
                    let discount_id3=''
                    let product=[]
                    if(that.data.cate_id1=='162'||that.data.cate_id1==162){
                        if(that.data.product.length==1){
                            discount_id=that.data.attrInfo.id
                            product=[]
                            discount_id2=''
                            discount_id3=''
                            is_train=is_train
                            discount_name=that.data.attrInfo.frist_name
        
                        }else{
                            if(is_train==3){
                                discount_id2=that.data.attrInfo.id
                                discount_id3=''
                            }else{
                                discount_id2=''
                                discount_id3=that.data.attrInfo.id
                            }
                            discount_id=0
                            product=that.data.product
                            discount_name=that.data.attrInfo.frist_name
                        }
                    }else{
                        if(that.data.cate_id1=='165'||that.data.cate_id1==165){
                            is_train=1
                        }else{
                            is_train=0
                        }
                        discount_id=that.data.attrInfo.id
                        product=[]
                        discount_id2=''
                        discount_name=that.data.attrInfo.frist_name+that.data.attrInfo.second_name
        
                    }
                    var params = {
                        area_id: that.data.area_id,
                        ucp_id: that.data.ucp_id, //	是	int	缴费记录表ID
                        course_id: that.data.course_id, //	是	int	课程ID
                        specs_id: that.data.specs_id, //	否	int	规格ID
                        discount_id: discount_id, //	否	int	折扣ID
                        discounts: that.data.attrInfo.price, //	是	int	折扣价
                        oto_type: moneyfree_id, //	是	int	付款方式ID
                        pay_method: pay_method, //	否	int	缴费方式 如果oto_type值为1 该参数必传
                        y_discounts: that.data.attrInfo.price, //	是	int	课程原价
                        account_paid: amount1, //	是	int	付款金额(包含学杂费)
                        jietu: str, //	是	string	截图 如果oto_type值为1 该参数必传
                        note: textarea, //	是	string	备注
                        wkOrderId: that.data.ordertext,
                        product:product,
                        discount_name:discount_name,
                        is_train:is_train,
                        discount_id2:discount_id2,
                        discount_id3:discount_id3
                    }
                    // if (Number(app.globalData.identity) == 3) {
                    //     if (that.data.newconid != '' && Number(that.data.newconid) > 0) {
                    //         var url = 'crmapi/user/againCourse';
                    //     } else {
                    //         var url = 'crmapi/Learn/againCourse';
                    //     }
                    // } else {
                    //     var url = 'crmapi/user/againCourse';
                    // }
                    var url = 'crmapi/Market/againCourse';
                    that.insertUserCourseNew(params, moneyfree_id, url, 1);
                }
            }
        } else {
            that.setData({
                butfalse: false,
            })
            wx.showLoading({
                title: '提交中~',
                mask: true
            })
            let discount_id=''
            let discount_name=''
            let is_train=that.data.is_train
            let discount_id2=''
            let discount_id3=''
            let product=''
            if(that.data.cate_id1=='162'||that.data.cate_id1==162){
                if(that.data.product.length==1){
                    discount_id=that.data.attrInfo.id
                    product=[]
                    discount_id2=''
                    discount_id3=''
                    is_train=is_train
                    discount_name=that.data.attrInfo.frist_name

                }else{
                    if(is_train==3){
                        discount_id2=that.data.attrInfo.id
                        discount_id3=''
                    }else{
                        discount_id2=''
                        discount_id3=that.data.attrInfo.id
                    }
                    discount_id=0
                    product=that.data.product
                    discount_name=that.data.attrInfo.frist_name
                }
            }else{
                if(that.data.cate_id1=='165'||that.data.cate_id1==165){
                    is_train=1
                }else{
                    is_train=0
                }
                discount_id=that.data.attrInfo.id
                product=[]
                discount_id2=''
                discount_name=that.data.attrInfo.frist_name+that.data.attrInfo.second_name

            }
            var params = {
                user_id: parseInt(that.data.stu_id), //	是	int	用户ID
                area_id: that.data.area_id,
                course_id: that.data.course_id, //	是	int	课程ID
                specs_id: that.data.specs_id, //	否	int	规格ID
                discount_id: discount_id, //	否	int	折扣ID
                discounts: that.data.attrInfo.price, //	是	int	折扣价
                oto_type: moneyfree_id, //	是	int	付款方式ID
                pay_method: pay_method, //	否	int	缴费方式 如果oto_type值为1 该参数必传
                y_discounts: that.data.attrInfo.price, //	是	int	课程原价
                account_paid: amount1, //	是	int	付款金额(包含学杂费)
                jietu: str, //	是	string	截图 如果oto_type值为1 该参数必传
                note: textarea, //	是	string	备注
                wkOrderId: that.data.ordertext,
                admin_id: app.globalData.admin_id,
                photo:that.data.photo?that.data.photo:that.data.uploadPhoto,
                product:product,
                discount_name:discount_name,
                is_train:is_train,
                discount_id2:discount_id2,
                discount_id3:discount_id3
            }
            params['consultant_id'] = app.globalData.admin_id;
            var url = 'crmapi/Market/addCourse'
            // if (Number(app.globalData.identity) == 3) {//学管
            //     // is_oneself   0-不在自己名下 1-自己名下
            //     if (that.data.is_oneself == 0 ) {//课程顾问id
            //         params['consultant_id'] = app.globalData.admin_id;
            //         if (Number(that.data.is_check) == 0) {//is_check=1，可转移，0，不可转移
            //             var url = 'crmapi/Learn/addCourseNew'
            //         } else {
            //             var url = 'crmapi/Learn/addCourse'
            //         }
            //     } else {
            //         params['consultant_id'] = app.globalData.admin_id;
            //         var url = 'crmapi/Learn/addCourse'
            //     }
            // } else {//课程顾问
            //     // is_oneself   0-不在自己名下 1-自己名下
            //     if (that.data.is_oneself == 0 ) {
            //         params['consultant_id'] = app.globalData.admin_id;
            //         if (Number(that.data.is_check) == 0) {//is_check=1，可转移，0，不可转移
            //             var url = 'crmapi/user/addCourseNew'
            //         } else {
            //             var url = 'crmapi/user/addCourse'
            //         }
            //     } else {
            //         params['consultant_id'] = app.globalData.admin_id;
            //         var url = 'crmapi/user/addCourse'
            //     }
            // }
            that.insertUserCourseNew(params, moneyfree_id, url, 2);
        }
        if (textarea != '') {
            that.setData({
                textsan: textarea,
            })
        }
        // 
    },


    // 线上
    insertUserCourseNew(params, type, url, typestatus) {
        //   type 1线上  2订单号 
        var that = this;
        var pay_method = that.data.pay_methods[that.data.pay_method_index].id;
        share.wxRequest(url, params, function(res) {
            var data = res.data;
            if (Number(data.errcode) == 0) {
                if(pay_method==19){
                    that.setData({
                        issuccess: true,
                        isfais: false,
                    })
                    
                }else{
                    if (Number(type) == 2) {
                        if (Number(typestatus) == 2) {
                            var jiaoyanCode = CONFIG.API_PIC + data.data.qr_img;
                        } else {
                            var jiaoyanCode = CONFIG.API_PIC + data.data.data.qr_img;
                        }
                        that.setData({
                            jiaoyanCode: jiaoyanCode,
                            showerca: true,
                            showerca1: true,
                        })
                    } else {
                        that.setData({
                            showfalse1: true,
                        })
                    }
                }
                wx.hideLoading();

            } else {
                wx.hideLoading();
                if(pay_method==19){
                    that.setData({
                        issuccess: false,
                        isfais: true,
                        text: data.msg,
                    })
                }else{
                    that.setData({
                        text: data.msg,
                        showfalse: true,
                    })
                }
            }
        })
    },

    sureclick() {
        var that = this;
        that.setData({
            butfalse: true,
            showfalse: false,
        })
    },
    sureclick1() {
        var that = this;
        wx.setStorageSync('followtype', 3);
        wx.navigateBack();
    },
    linkreportclick(e){
        var id = e.currentTarget.dataset.id || e.target.dataset.id;
        var courselist=this.data.courselist
        // courselist[0].type=id
        this.setData({
            childtype:id,
            courselist:courselist
        })
    },
})